package com.roast.services.my;

import com.roast.util.QueryObject;
import com.roast.domain.*;
import com.roast.util.QueryBeanFactory;

public class EditIMPL extends GeneralSO implements Edit {

    private UserData user;
    private String newNickname;
    private String newPassword;

    private QueryObject queryObject;
    private DomainObject fromDB;

    @Override
    public int edit(UserData user) {
        return new EditIMPL().generalExecution(user);
    }

    @Override
    protected int executeSO(Object obj) {
        if (!setUser(obj)) {
            return 10401;
        }
        if (!user.isEmailSet()) {
            return 10402;
        }
        if (!checkPasswordSet()) {
            return 10403;
        }
        if (!checkNicknameSet()) {
            return 10404;
        }
        if (!setQuery()) {
            return 10405;
        }
        if (!isUserExist()) {
            return 10406;
        }
        if (!setUser(fromDB)) {
            return 10401;
        }
        if (!updateUser()) {
            return 10407;
        }
        if (!copyObject(obj)) {
            return 10408;
        }
        return 10400;
    }

    private boolean setUser(Object obj) {
        try {
            this.user = (UserData) obj;
            return true;
        } catch (ClassCastException ex) {
            return false;
        }
    }

    private boolean checkPasswordSet() {
        if (user.getPassword() == null || user.getPassword().equals("")) {
            return false;
        }
        newPassword = user.getPassword();
        return true;
    }

    private boolean checkNicknameSet() {
        if (user.getNickname() == null || user.getNickname().equals("")) {
            return false;
        }
        newNickname = user.getNickname();
        return true;
    }

    private boolean setQuery() {
        queryObject = QueryBeanFactory.getBean("readUserByParametar");

        if (queryObject == null) {
            return false;
        }

        queryObject.addParametar("femail", user.getEmail());
        return true;
    }

    private boolean isUserExist() {
        fromDB = dbbr.readOneByParametar(queryObject);
        return queryObject.getSignal() == 10030;
    }

    private boolean updateUser() {
        user.setNickname(newNickname);
        user.setPassword(newPassword);
        return dbbr.update(user);
    }

    private boolean copyObject(Object obj) {
        try {
            return ((UserData) obj).copyObject(user);
        } catch (ClassCastException ex) {
            return false;
        }
    }

    @Override
    protected boolean executeState(int signal) {
        switch (signal) {
            case 10400:
                showOperationState(signal, "edit: You have successfully edit user data.");
                return true;
            case 10401:
                showOperationState(signal, "edit:ERROR: Class cast exception.");
                return false;
            case 10402:
                showOperationState(signal, "edit:ERROR: You did not set user email address.");
                return false;
            case 10403:
                showOperationState(signal, "edit:ERROR: You did not set user password.");
                return false;
            case 10404:
                showOperationState(signal, "edit:ERROR: You did not set user nickname.");
                return false;
            case 10705:
                showOperationState(signal, "edit:ERROR: Fail to set query.");
                return false;
            case 10406:
                showOperationState(signal, "edit:ERROR: User dose not exist.");
                return false;
            case 10407:
                showOperationState(signal, "edit:ERROR: User data was not updated in database.");
                return false;
            case 10708:
                showOperationState(signal, "edit:ERROR: Fail to copy object.");
                return false;
        }
        return false;
    }

}
